home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / 13HLIB.ZIP / 13HLIB.DOX < prev    next >
Encoding:
Text File  |  1996-08-10  |  15.9 KB  |  394 lines

  1. User's Manual for Mode13hLib                 Coded by Pri$m 
  2. ────────────────────────────                 ──────────────
  3.  
  4. Contents
  5. ────────
  6.  
  7.   Note from the Author
  8.  
  9. 1 Notes on the usage of Mode13hLib
  10. ·    Error checking
  11. ·    Sprites
  12. ·    Page Flipping
  13. ·    The Flip Page and out of bounds Sprites.
  14.  
  15. 2 Structures and typedefs
  16. ·    ColorStruct
  17. ·    Sprite
  18.  
  19. 3 Member Functions
  20. ·    SetMode13h
  21. ·    CloseMode13h
  22. ·    DetectVGA
  23. ·    ClearScreen
  24. ·    PlotPixel
  25. ·    GetPixel
  26. ·    GetPaletteColor
  27. ·    SetPaletteColor
  28. ·    WaitVerticalRetrace
  29. ·    LoadPCXFile
  30. ·    LoadPalette
  31. ·    SavePalette
  32. ·    HorizontalLine
  33. ·    VerticalLine
  34. ·    Bar
  35. ·    Rectangle
  36. ·    GetSprite
  37. ·    DrawSprite
  38. ·    DrawSpriteNoTrans
  39. ·    WriteSpriteToDisk
  40. ·    ReadSpriteFromDisk
  41. ·    SetUpPage
  42. ·    ClosePage
  43. ·    PageActive
  44. ·    CopyPageToScreen
  45. ·    BltText
  46. ·    Line
  47.  
  48. 4 Example programs
  49. ·    Palette Demo
  50. ·    Sprite Demo
  51.  
  52. 5 Special notes on using page flipping
  53. 6 Greetings to ...
  54.  
  55.  
  56. «────────────────────────────────────────────────────────────────────────────»
  57.  
  58. Note from the Author
  59. ────────────────────
  60.  
  61. Greetings!
  62. ──────────
  63.  
  64. Mode13hLib is an easy to use, fast graphics library that  (yeah, you guessed
  65. it!) operates in Mode 13h. (19 in decimal) This library is primarily for
  66. those who want to break free from the restrictions of BGI (slow, bulky, only
  67. 16 colors...) and enter the world of fast, 256 color graphics, without going
  68. to the often impractical (especially for the hobby programmer) lengths of
  69. writing their own library. The source code is also there to be disected
  70. should you want to; feel free to rip code from it should you need to.
  71.  
  72. Mode13hLib is constructed in an object-oriented way, so that the complexity
  73. of many of the functions is hidden from the user. It also becomes very easy
  74. to conceptualise - all the functions belong to one, discreet object, they are
  75. not just scattered around. Also, this means that if you would like to add on
  76. to the library, you can easily derive a class from it and add your own
  77. functions and variables to it.
  78.  
  79. Mode13hLib is FreeWare. That means you can give it to all your friends,
  80. upload it to all the BBSs you like, and use it in any program you like.
  81. That's what I wrote it for. There are, however, two conditions. The first is
  82. that you do not remove my name and copyright from the code. It is not
  83. visible to the user, but is hidden inside the .exe file. Also, when you
  84. release any programs you've written with Mode13hLib, send me a letter
  85. (PO Box 2412, Bedfordview 2008, South Africa), or email me (prism@pixie.co.za
  86. or prism@digitec.co.za) telling me a bit about it. Mention in the
  87. documentation is optional, but would be kewl!
  88.  
  89. Above all, have phun, and remember, the only limit is your imagination!
  90.  
  91. Regards,
  92.        Pri$m.
  93.  
  94. «────────────────────────────────────────────────────────────────────────────»
  95.  
  96. Notes on the usage of Mode13hLib
  97. ────────────────────────────────
  98.  
  99. ·    Error checking.
  100. All functions in Mode13hLib that obtain a Sprite allocate the memory for that
  101. Sprite using farmalloc(). (If you don't know what a Sprite is yet, don't
  102. worry, it's coming up) Make sure that you KillSprite them when finished with
  103. them. E.g.: KillSprite(SpriteName); This should also be done when you want
  104. to replace an already used Sprite.
  105.  
  106. (KillSprite is a macro, with KillSprite(x) defined as
  107. farfree(x))
  108.  
  109. Also, those function return values to indicate either SUCCESS or FAILURE.
  110. Please check these values. The functions (especially for Sprites) that are
  111. speed-critical do NOT check if you pass them a NULL pointer. HorizontalLine
  112. does NOT check if you happen to pass it a negative argument. Please be
  113. careful in this regard, as the results may be unpredictable. I have tried
  114. using Bar() with a negative value, and it crashed my program.
  115. Please be careful with this.
  116.  
  117. ·    Sprites
  118. Sprites are bitmapped images that are usually moved around the screen at a
  119. rapid pace. They could be a spaceman (as in the example) or a ball. They are
  120. to be used using the Sprite data type. There are two ways to draw a Sprite :
  121. With transparent pixels, and without. Transparent pixels are pixels with a
  122. value of 0. They are, therefore, not drawn on the screen. This means that
  123. although a Sprite is stored in a rectangle, it may be round, or have a hole
  124. in the middle. Without transparent pixels, ALL of the pixels on the Sprite
  125. are drawn to the screen. This tends to be slightly faster than using
  126. transparent pixels, but shapes other than squares, which are on a background,
  127. present a problem. As an example of this, if the Spaceman was drawn using
  128. no transparent pixels, the stars close to him, that you can see in the demo,
  129. for instance right next to his helmet, would not be visible.
  130.  
  131. ·    Page flipping
  132. You may have noticed that sometimes when drawing on the screen, the image
  133. flickers. This often happens when doing a lot of drawing. One solution to
  134. this problem is WaitVerticalRetrace, which waits until Vertical Retrace (the
  135. period when your monitor is not refreshing the screen) is occurring. Another
  136. method goes hand in hand with WaitVerticalRetrace : Instead of drawing to the
  137. screen, why not draw everything to a copy of the screen in memory, and then
  138. copy it all across to the screen in one shot, saving time and flicker.
  139. When you enable this option, all drawing is done to this copy. You must call
  140. the function CopyPageToScreen to copy your image to the screen. The penalty
  141. for this is that the amount of memory required to store an entire screen's
  142. worth of pixels is about 64K. I left the choice up to you.
  143.  
  144. The Flip Page and out of bounds Sprites.
  145. ───────────────────────────────────────
  146. Be extremely careful when drawing on the Flip Page. (Which will happen if you
  147. say SetupPage()) If you draw a Sprite out of bounds, (off the page), memory
  148. located in and around the flip page will become corrupted. DO NOT do this.
  149. Experiments with it have caused my system to crash, or at best just bombed
  150. out the program. Be careful and include error checking here. It's fine to
  151. write over the side of the page, but not over the top or the bottom.
  152.  
  153. «────────────────────────────────────────────────────────────────────────────»
  154.  
  155. Structures and typedefs
  156. ───────────────────────
  157. ·    ColorStruct{
  158.  unsigned char r;
  159.  unsigned char g;
  160.  unsigned char b;
  161. };
  162.  
  163. ColorStruct is the structure defined for palette manipulation. It contains
  164. three values, one for each of the three components of any color on the screen.
  165. Each value can be only 0-63, otherwise it will be ignored if you try and
  166. set the palette using it. (Each color pixel is made up of a certain
  167. combination of these value. E.g. Dark Grey: r=3; b=63; g=63;, and Black: r=0;
  168. g=0; b=0;)
  169.  
  170. ·    typedef char far *Sprite;
  171.   This typedef merely disguises the fact that a Sprite is stored as an array
  172. of characters, (Almost the same way as a string [typedef unsigned char far*
  173. Sprite]). All Sprite functions take this data type as arguments. When you
  174. declare a Sprite, simply say "Sprite MySprite". To get a Sprite from a
  175. function that returns a Sprite, just say:
  176. "MySprite = Mode13h.ReadSpriteFromDisk("mysprite.m13");" Everything is coded
  177. in this way, and this makes Sprite handling extremely easy. Just treat them
  178. as any other data type.
  179.  
  180. «────────────────────────────────────────────────────────────────────────────»
  181.  
  182. Member Functions
  183. ────────────────
  184.  
  185. void SetMode13h()
  186. ─────────────────
  187. Sets up VGA graphics mode 13h. (320x200x256)
  188.  
  189. void CloseMode13h()
  190. ───────────────────
  191. Resets the monitor mode back to text mode.
  192.  
  193. int DetectVGA()
  194. ───────────────
  195. Detects the presence of a VGA card on the system. Returns SUCCESS if there is
  196. one, or FAILURE if there isn't.
  197.  
  198. void ClearScreen(unsigned char color)
  199. ─────────────────────────────────────
  200. Clears the screen with the given color.
  201.  
  202. void PlotPixel(int x, int y, unsigned char color)
  203. ─────────────────────────────────────────────────
  204. Places one pixel at x across, y down, with the given color.
  205.  
  206. unsigned char GetPixel(int x, int y)
  207. ────────────────────────────────────
  208. Returns the color of the pixel at the specified location.
  209.  
  210. void GetPaletteColor(int index, ColorStruct& Colstr)
  211. ────────────────────────────────────────────────────
  212. Places the information from the palette entry index into the ColorStruct
  213. Colstr.
  214.  
  215. void SetPaletteColor(int index, ColorStruct &Colstr)
  216. ────────────────────────────────────────────────────
  217. Copies the color information in Colstr into the palette entry specified by
  218. index.
  219.  
  220. void WaitVerticalRetrace()
  221. ──────────────────────────
  222. Waits until the monitor is in a Vertical Retrace period. This makes it easier
  223. to draw to the screen without worrying about flicker. For flickerless drawing,
  224. combine this with the use of a flip page. (Facilitates are provided for that -
  225. see SetUpPage(). Call this before calling CopyPageToScreen() and you won't
  226. ever have to worry about flicker.)
  227.  
  228. int LoadPCXFile(char *name)
  229. ───────────────────────────
  230. Loads the PCX file specified by name to the screen. (Or swap page.) Returns
  231. FAILURE if the PCX file could not be found or is not a Mode13h PCX file.
  232. (320x200x256, 8 bits per pixel, etc. A good program to use for these is
  233. Improces by John Wagner. (ShareWare)). Otherwise returns SUCCESS.
  234.  
  235. int LoadPalette(char *filename)
  236. ───────────────────────────────
  237. Loads a palette from the given filename. If the file could not be opened,
  238. returns FAILURE. Otherwise, returns SUCCESS. Note that this function is not
  239. only able to read palette files from the function SavePalette, but also from
  240. the ends of Mode13h PCX files.
  241.  
  242. int SavePalette(char *filename)
  243. ───────────────────────────────
  244. Saves the current palette to the filename specified. If it could not open the
  245. file, returns FAILURE, else returns SUCCESS.
  246.  
  247. void HorizontalLine(int x, int y, int xx, unsigned char color)
  248. ──────────────────────────────────────────────────────────────
  249. Draws a horizontal line from x,y to xx,y in the specified color.
  250.  
  251. void VerticalLine(int x, int y, int yy, unsigned char color)
  252. ────────────────────────────────────────────────────────────
  253. Draws a vertical line from x,y to x,yy in the given color.
  254.  
  255. void Bar(int x, int y, int width, int height, unsigned char color)
  256. ──────────────────────────────────────────────────────────────────
  257. Draws a filled in rectangle in the color specified from x,y to x+width,
  258. y+height.
  259.  
  260. void Rectangle(int x, int y, int width, int height, unsigned char color)
  261. ────────────────────────────────────────────────────────────────────────
  262. Draw a rectangle is the specified color from x,y to x+width, y+height.
  263.  
  264. Sprite GetSprite(int x, int y, int width, int height)
  265. ─────────────────────────────────────────────────────
  266. Gets a Sprite from the specified location with the specified width and height
  267. and returns it.
  268.  
  269. void DrawSprite(int x, int y, int width, int height, Sprite Sprte)
  270. ──────────────────────────────────────────────────────────────────
  271. Draws the given Sprite (Sprte) at the given co-ordinates on the screen, with
  272. the given width and height. It ignores all pixels with value 0, thereby
  273. making them `transparent'.
  274.  
  275. void DrawSpriteNoTrans(int x, int y, int width, int height, Sprite Sprte)
  276. ─────────────────────────────────────────────────────────────────────────
  277. Draws the given Sprite (Sprte) in the same way as DrawSprite() except that it
  278. does not ignore pixels with a value of 0. It is slightly faster than
  279. DrawSprite().
  280.  
  281. int WriteSpriteToDisk(char *filename, int width, int height, Sprite Sprte)
  282. ──────────────────────────────────────────────────────────────────────────
  283. Writes the specified Sprite to the specified file on disk. Returns FAILURE if
  284. the Sprite could not be written, otherwise it returns SUCCESS. If the file
  285. already exists, it is overwritten.
  286.  
  287. Sprite ReadSpriteFromDisk(char *filename)
  288. ─────────────────────────────────────────
  289. Reads a Sprite from the specified file and returns it.
  290.  
  291. void SetUpPage()
  292. ────────────────
  293. Sets up a swap page in memory. A swap page is like a `virtual screen', and
  294. when you draw, it is drawn to. After that, you must call CopyPageToScreen()
  295. to copy it to the actual screen. Using a swap page reduces flicker. (and with
  296. WaitVerticalRetrace eliminates it) It does, however, use 64K of memory.
  297.  
  298. void ClosePage()
  299. ────────────────
  300. Closes down the swap page. All drawing is now redirected to the screen. Do
  301. this always before exiting a program that uses a swap page.
  302.  
  303. int PageActive()
  304. ────────────────
  305. Returns whether or not the swap page is active. If it is, it returns SUCCESS,
  306. otherwise it returns FAILURE. PageActive returning FAILURE after you have
  307. called SetUpPage() (But not ClosePage()) indicates that the system did not
  308. have enough memory for it.
  309.  
  310. void CopyPageToScreen()
  311. ───────────────────────
  312. Copies the contents of the swap page to screen in one go.
  313.  
  314. Always call this if you are using a swap page and you want
  315. what you drew displayed.
  316.  
  317. void BltText(char *text, int x, int y, unsigned char color)
  318. ───────────────────────────────────────────────────────────
  319. Prints out the specified string at the given co-ordinates (x, y), in the
  320. given color.
  321.  
  322. void Line(int x1, int y1, int x2, int y2, unsigned char color)
  323. ──────────────────────────────────────────────────────────────
  324. Draws a line from x1,y1 to x2,y2 in the given color.
  325.  
  326. «────────────────────────────────────────────────────────────────────────────»
  327.  
  328. Example programs
  329. ────────────────
  330. ·    Palette Demo (paldemo.exe)
  331. The Palette demo is a short demonstration of the effects that can be achieved
  332. by simple manipulation of the palette. It uses a couple of loops to set the
  333. palette colors into 64 shades of red, 64 shades of blue, 64 shades of green
  334. and about 50 shades of grey. It then draws a couple of lines, in ascending
  335. order of the palette, which are slowed down for visual effect. (Note that had
  336. they not been slowed down, the drawing would be done instantly, with no
  337. visual indication) It then draws on the extra shades of grey, and fades out.
  338. (A nice little palette trick to accomplish a seemingly hard effect easily)
  339.  
  340. ·    Sprite demo (sprdemo.exe)
  341. The Sprite demo is another small demo I wrote to demonstrate the Sprite
  342. handling capabilities of Mode13hLib. It draws a random series of stars, and
  343. then `walks' a pre-saved animation of a spaceman up the screen. At the same
  344. time it also bounces some text along the top of the screen. It took very few
  345. lines of code to create a working, hi-speed (it needed a delay to look right)
  346. demo.
  347.  
  348. «────────────────────────────────────────────────────────────────────────────»
  349.  
  350. Special Notes on Using Page Flipping.
  351. ────────────────────────────────────
  352. Page Flipping is enabled by calling Mode13hLib.SetUpPage(). It can be
  353. disabled by calling Mode13hLib.ClosePage(). Please always check that the page
  354. you've tried to create was actually created. Do this by calling
  355. Mode13hLib.PageActive(). The function will return SUCCESS if it is. FAILURE
  356. means that the system did not have the 64K of memory needed for the swap page.
  357. This is extremely important, because if after that you try and
  358. CopyPageToScreen(), the results are definitely going to be messy.
  359.  
  360. «────────────────────────────────────────────────────────────────────────────»
  361.  
  362. Greetings to ...
  363. ────────────────
  364. Greetings to the following:
  365.  
  366. The rest of The BRiGADE:
  367.  - BLiTZ
  368.  - MATRiX
  369.  - MERLyN
  370.  - HoTRod
  371.  - HaRdRocK
  372.  
  373. - CD & SX
  374. - Elquin   (Are we having fun yet ... ?!)
  375. - JYnXx    << Black MagiK BBS ((011) 453-8494) >>
  376. - ReVerB
  377. - HACKERJACk
  378. - Grystal
  379. - Psycho Bunny
  380. - Gumpf
  381.  
  382. - Special thanx to Amit J Patel, who runs a waaay kewl Games Programming Page
  383.  at http://www-cs-students.stanford.edu/~amitp/gameprog.html, for help
  384.  on finding a Line algorithm. His help was much appreciated!!
  385.  
  386. «────────────────────────────────────────────────────────────────────────────»
  387.          "He rides the wild October sky;
  388.           He shall not die;
  389.           He shall not die."
  390.  - The ScareCrows.
  391. «────────────────────────────────────────────────────────────────────────────»
  392.  
  393.  
  394.